package com.bhs.sansong.ui.mine;

import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bhs.sansong.R;
import com.bhs.sansong.base.BaseActivity;
import com.bhs.sansong.base.BaseBean;
import com.bhs.sansong.net.NetCallBack;
import com.bhs.sansong.utils.MyUtils;
import com.bhs.sansong.utils.ToastUtil;
import com.google.gson.Gson;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 修改内容
 */
public class EditTextActivity extends BaseActivity implements NetCallBack {


    private EditText mEtContent;
    private Button mBtnSave;
    private String updateType;
    private TextView mTvHint;
    private String hint;

    @Override
    public int initLayout() {
        return R.layout.activity_edit_text;
    }

    @Override
    public void initView() {
        mEtContent = findViewById(R.id.et_content);
        mBtnSave = findViewById(R.id.btn_save);
        mEtContent.setText(getIntent().getStringExtra("Content"));
        mTvHint = findViewById(R.id.tv_hint);
        updateType = getIntent().getStringExtra("updateType");
        TextView mTvTitle = findViewById(R.id.tv_title);
        if (updateType.equals("nickname")) {
            mTvTitle.setText("修改昵称");
            mTvHint.setText("输入新的昵称");
            hint = "昵称不能为空";
        }else if(updateType.equals("company_name")){
            mTvTitle.setText("修改公司名称");
            mTvHint.setText("输入新的公司名称");
            hint = "公司名称不能为空";
        }else if(updateType.equals("company_address")){
            mTvTitle.setText("修改公司地址");
            mTvHint.setText("输入新的修改公司地址");
            hint = "修改公司地址不能为空";
        }
        mEtContent.setFilters(MyUtils.inputFilters(this, 100));
    }

    @Override
    public void initData() {
        mBtnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(mEtContent.getText().toString())) {
                    ToastUtil.show(hint);
                    return;
                }
                networkRequest(netApi.update_user_info(updateType, mEtContent.getText().toString()), EditTextActivity.this);
            }
        });
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        BaseBean bean = new Gson().fromJson(json, BaseBean.class);
        ToastUtil.show(bean.getMsg());
        if (bean.isStatus()) {
            finish();
        }
    }

    @Override
    public void onFailure(int requestCode) {

    }
}